//
//  MBProgressHUD+ProgressHUD.h
//  jiedabao
//
//  Created by 李小龙 on 2017/3/20.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "MBProgressHUD+ProgressHUD.h"
#import <ImageIO/ImageIO.h>

@implementation MBProgressHUD (ProgressHUD)

+ (MBProgressHUD*)createMBProgressHUDviewWithMessage:(NSString*)message isWindiw:(BOOL)isWindow
{
    UIView  *view = isWindow? (UIView*)[UIApplication sharedApplication].delegate.window:[self getCurrentUIVC].view;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message?message:kDefaultMessage;
    hud.label.font =[UIFont systemFontOfSize:15];
    hud.removeFromSuperViewOnHide = YES;
//    hud.dimBackground = NO;
    return hud;
}
#pragma mark-------------------- show Tip----------------------------

+ (void)showTipMessageInWindow:(NSString*)message
{
    [self showTipMessage:message isWindow:true timer:kDefaultTime];
}
+ (void)showTipMessageInView:(NSString*)message
{
    [self showTipMessage:message isWindow:false timer:kDefaultTime];
}
+ (void)showTipMessageInWindow:(NSString*)message timer:(int)aTimer
{
    [self showTipMessage:message isWindow:true timer:aTimer];
}
+ (void)showTipMessageInView:(NSString*)message timer:(int)aTimer
{
    [self showTipMessage:message isWindow:false timer:aTimer];
}
+ (void)showTipMessage:(NSString*)message isWindow:(BOOL)isWindow timer:(int)aTimer
{
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:kDefaultTime];
}
#pragma mark-------------------- show Activity----------------------------

+ (void)showActivityMessageInWindow:(NSString*)message
{
    [self showActivityMessage:message isWindow:true timer:0];
}
+ (void)showActivityMessageInView:(NSString*)message
{
    [self showActivityMessage:message isWindow:false timer:0];
}
+ (void)showActivityMessageInWindow:(NSString*)message timer:(int)aTimer
{
    [self showActivityMessage:message isWindow:true timer:aTimer];
}
+ (void)showActivityMessageInView:(NSString*)message timer:(int)aTimer
{
    [self showActivityMessage:message isWindow:false timer:aTimer];
}
+ (void)showActivityMessage:(NSString*)message isWindow:(BOOL)isWindow timer:(int)aTimer
{
    MBProgressHUD *hud  =  [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.mode = MBProgressHUDModeIndeterminate;
    if (aTimer>0) {
        [hud hideAnimated:YES afterDelay:aTimer];
    }
}
#pragma mark-------------------- show Image----------------------------

+ (void)showSuccessMessage:(NSString *)Message
{
    
    [self showCustomIconInWindow:@"MBHUD_Success" message:Message timer:kDefaultTime];
}

+ (void)showSuccessMessage:(NSString *)Message timer:(int)aTimer
{
    [self showCustomIconInWindow:@"MBHUD_Success" message:Message timer:aTimer];
}



+ (void)showErrorMessage:(NSString *)Message
{
//    if (![BIZUserModel shareUser].login) {
//        return;
//    }
    [self showCustomIconInWindow:@"MBHUD_Error" message:Message timer:kDefaultTime];
}

+ (void)showErrorMessage:(NSString *)Message timer:(int)aTimer
{
    [self showCustomIconInWindow:@"MBHUD_Error" message:Message timer:aTimer];
}


+ (void)showInfoMessage:(NSString *)Message
{
    [self showCustomIconInWindow:@"MBHUD_Info" message:Message timer:kDefaultTime];
}

+ (void)showInfoMessage:(NSString *)Message timer:(int)aTimer
{
    [self showCustomIconInWindow:@"MBHUD_Info" message:Message timer:aTimer];
}

+ (void)showWarnMessage:(NSString *)Message
{
    [self showCustomIconInWindow:@"MBHUD_Warn" message:Message timer:kDefaultTime];
}

+ (void)showWarnMessage:(NSString *)Message timer:(int)aTimer
{
    [self showCustomIconInWindow:@"MBHUD_Warn" message:Message timer:aTimer];
}


+ (void)showCustomIconInWindow:(NSString *)iconName message:(NSString *)message
{
    [self showCustomIcon:iconName message:message isWindow:true timer:kDefaultTime];
    
}

+ (void)showCustomIconInWindow:(NSString *)iconName message:(NSString *)message  timer:(int)aTimer
{
    [self showCustomIcon:iconName message:message isWindow:true timer:aTimer];
}

+ (void)showCustomIconInView:(NSString *)iconName message:(NSString *)message
{
    [self showCustomIcon:iconName message:message isWindow:false timer:kDefaultTime];
}

+ (void)showCustomIconInView:(NSString *)iconName message:(NSString *)message timer:(int)aTimer
{
    [self showCustomIcon:iconName message:message isWindow:false timer:aTimer];
}

+ (void)showCustomIcon:(NSString *)iconName message:(NSString *)message isWindow:(BOOL)isWindow  timer:(int)aTimer
{
    [MBProgressHUD hideHUD];
    
    MBProgressHUD *hud  =  [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
    hud.mode = MBProgressHUDModeCustomView;
    [hud hideAnimated:YES afterDelay:aTimer];
}


+ (void)hideHUD
{
    UIView  *winView =(UIView*)[UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:winView animated:YES];
    [self hideHUDForView:[self getCurrentUIVC].view animated:YES];
}


+ (void)hideHubAfter:(NSTimeInterval)time
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
        
        [self hideHUD];
        
    });
}


//获取当前屏幕显示的viewcontroller
+(UIViewController *)getCurrentWindowVC
{
    UIViewController *result = nil;
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tempWindow in windows)
        {
            if (tempWindow.windowLevel == UIWindowLevelNormal)
            {
                window = tempWindow;
                break;
            }
        }
    }
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
    {
        result = nextResponder;
    }
    else
    {
        result = window.rootViewController;
    }
    return  result;
}
+(UIViewController *)getCurrentUIVC
{
    UIViewController  *superVC = [[self class]  getCurrentWindowVC ];
    
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }
    return superVC;
}


+ (void)showAnimationHUD
{
    [MBProgressHUD hideHUD];
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"wait" withExtension:@"gif"];//加载GIF图片
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);//将GIF图片转换成对应的图片源
    size_t imagesCount=CGImageSourceGetCount(gifSource);//获取其中图片源个数，即由多少帧图片组成
    NSMutableArray* imagesArr = [[NSMutableArray alloc] init];//定义数组存储拆分出来的图片
    for (size_t i=0; i < imagesCount; i++){
        CGImageRef imageRef=CGImageSourceCreateImageAtIndex(gifSource, i, NULL);//从GIF图片中取出源图片
        UIImage* imageName=[UIImage imageWithCGImage:imageRef scale:2 orientation:UIImageOrientationUp];//将图片源转换成UIimageView能使用的图片源
        [imagesArr addObject:imageName];//将图片加入数组中
        CGImageRelease(imageRef);
    }
    
    UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 112, 112)];
    imageview.animationImages = imagesArr;//将图片数组加入UIImageView动画数组中
    imageview.animationDuration = 3;//每次动画时长
    [imageview startAnimating];//开启动画，此处没有调用播放次数接口，UIImageView默认播放次数为无限次，故这里不做处理
    MBProgressHUD *HUD  =  [self createMBProgressHUDviewWithMessage:nil isWindiw:YES];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    HUD.animationType = MBProgressHUDAnimationZoomIn;
    HUD.customView = imageview;
    HUD.bezelView.color = [UIColor clearColor];
    HUD.backgroundColor = RGBA(0, 0, 0, 0.5);
    [HUD showAnimated:YES];
}



+ (void)showProgressHorizontalMessage:(NSString*)message progress:(float)progress isWindow:(BOOL)isWindow timer:(int)aTimer
{
    [MBProgressHUD hideHUD];
    MBProgressHUD *hud  =  [self createMBProgressHUDviewWithMessage:message isWindiw:isWindow];
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    hud.progress = progress;
    if (aTimer>0) {
        [hud hideAnimated:YES afterDelay:aTimer];
    }
}


+ (void)showProgressWithMessage:(NSString*)message progress:(float)progress
{
    [MBProgressHUD showProgressHorizontalMessage:message progress:progress isWindow:YES timer:0];
}


+ (void)showBottomTipMessageInWindow:(NSString*)message
{
    MBProgressHUD *hud = [self createMBProgressHUDviewWithMessage:message isWindiw:YES];
    hud.mode = MBProgressHUDModeText;
    hud.offset =  CGPointMake(0.f, MBProgressMaxOffset);
    
    [hud hideAnimated:YES afterDelay:kDefaultTime];
}

@end
